package com.ruoyi.quartz.task;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.HyrjAppointmentMapper;
import com.ruoyi.system.mapper.HyrjReportMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {

    @Autowired
    private HyrjReportMapper hyrjReportMapper;
    @Autowired
    private IHyrjMachineService hyrjMachineService;
    @Autowired
    private IHyrjAppointmentActionService hyrjAppointmentActionService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private HyrjAppointmentMapper hyrjAppointmentMapper;
    @Autowired
    private IHyrjLaundryPatternService hyrjLaundryPatternService;


    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }


    public void updateReport() {
        //查询未出里的举报列表
        HyrjReport hyrjReport = new HyrjReport();
        hyrjReport.setReportStatus("1");
        List<HyrjReport> hyrjReportList = hyrjReportMapper.selectHyrjReportList(hyrjReport);

        for (HyrjReport report : hyrjReportList) {
            //根据被举报人 反向查出排队列表
            HyrjAppointmentAction hyrjAppointmentAction = new HyrjAppointmentAction();
            hyrjAppointmentAction.setUserId(hyrjReport.getReportedPersonId());
            List<HyrjAppointmentAction> hyrjAppointmentActions = hyrjAppointmentActionService.selectHyrjAppointmentActionList(hyrjAppointmentAction);
            //如果能查到 根据编码 将查到的洗衣机更新成消毒中
            if (!hyrjAppointmentActions.isEmpty()) {
                String machineNo = hyrjAppointmentActions.get(0).getMachineNo();
                HyrjMachine hyrjMachine = hyrjMachineService.selectHyrjMachineByNo(machineNo);
                if (null != hyrjMachine) {
                    hyrjMachine.setMachineStatus("4");
                    hyrjMachineService.updateHyrjMachine(hyrjMachine);
                }
            }
            //同时更新举报状态为已处理
            report.setReportStatus("2");
            hyrjReportMapper.updateHyrjReport(report);
        }

        //根据被举报人查出用户  同时更新举报次数+1 如果大于3 就封号
        SysUser sysUser = sysUserMapper.selectUserById(hyrjReport.getReportedPersonId());
        if (null != sysUser) {
            Integer reportNumber = null == sysUser.getReportNumber() ? 0 : sysUser.getReportNumber();
            sysUser.setReportNumber(reportNumber + 1);
            if (reportNumber >= 3) {
                sysUser.setStatus("1");
            }
            sysUserMapper.updateUser(sysUser);
        }

    }

    /**
     * 五分制执行一次  如果查询到预约但未使用 剔除队列
     */
    public void queueCulling() {
        //查询是未预约的
        HyrjAppointment hyrjAppointment = new HyrjAppointment();
        hyrjAppointment.setAppointmentStatus("0");
        List<HyrjAppointment> hyrjAppointmentList = hyrjAppointmentMapper.selectHyrjAppointmentList(new HyrjAppointment());


        for (HyrjAppointment hyrjAppointment1 : hyrjAppointmentList) {

            HyrjAppointmentAction hyrjAppointmentAction = new HyrjAppointmentAction();
            hyrjAppointmentAction.setUserId(hyrjAppointment1.getUserId());
            hyrjAppointmentAction.setMachineNo(hyrjAppointment1.getMachineNo());

            //如果查询到 5分之后剔除队列
            HyrjAppointmentAction hyrjAppointmentAction1 = hyrjAppointmentActionService.selectHyrjAppointmentActionByMachineNo(hyrjAppointmentAction);
            if (null != hyrjAppointmentAction1) {
                hyrjAppointmentActionService.deleteHyrjAppointmentActionById(hyrjAppointmentAction1.getId());
            }


            hyrjAppointment1.setAppointmentStatus("5");
            hyrjAppointmentMapper.updateHyrjAppointment(hyrjAppointment1);
        }

    }


    /**
     * 每分钟执行一次， 剩余时间为0时  将洗衣机更新成待使用
     */
    public void updateHyrjMachine() {
        List<HyrjAppointmentAction> hyrjAppointmentActionList = hyrjAppointmentActionService.selectHyrjAppointmentActionList(new HyrjAppointmentAction());

        Map<String, List<HyrjAppointmentAction>> hyrjAppointmentActionListMap = hyrjAppointmentActionList.stream()
                .collect(Collectors.groupingBy(HyrjAppointmentAction::getMachineNo));

        HyrjMachine hyrjMachine = new HyrjMachine();
        hyrjMachine.setMachineStatus("2");
        List<HyrjMachine> list = hyrjMachineService.selectHyrjMachineList(hyrjMachine);

        for (HyrjMachine machine : list) {

            List<HyrjAppointmentAction> hyrjAppointmentActionList1 = hyrjAppointmentActionListMap.get(machine.getMachineNo());

            if (null != hyrjAppointmentActionList1) {

                for (HyrjAppointmentAction hyrjAppointmentAction : hyrjAppointmentActionList1) {

                    if (null != hyrjAppointmentAction.getLaundryPattern()) {

                        HyrjLaundryPattern hyrjLaundryPattern = hyrjLaundryPatternService.selectHyrjLaundryPatternByLaundryPattern(hyrjAppointmentAction.getLaundryPattern());

                        if (null != hyrjLaundryPattern) {
                            Date appointmentTime = hyrjAppointmentAction.getLaundryStartTime();

                            // 将 laundry_pattern 转换为毫秒数
                            long laundryPatternDurationMillis = Integer.parseInt(hyrjLaundryPattern.getLaundryTime()) * 60 * 1000;

                            // 获取当前时间的毫秒数
                            long nowMillis = System.currentTimeMillis();

                            // 将 appointmentTime 转换为毫秒数
                            long appointmentTimeMillis = appointmentTime.getTime();

                            // 计算新的时间（appointmentTime 加上 laundry_pattern）的毫秒数
                            long newTimeMillis = appointmentTimeMillis + laundryPatternDurationMillis;

                            // 计算新的时间与当前时间的差值（毫秒数）
                            long timeDifferenceMillis = newTimeMillis - nowMillis;

                            // 如果 timeDifferenceMillis 是负数，表示当前时间已经超过了新的时间
                            if (timeDifferenceMillis > 0) {
                                // 这里通常不会直接将时间差转换为 Date，因为 Date 表示一个具体的时间点
                                // 而是可能会将时间差转换为小时、分钟等更容易理解的单位
                                long hours = timeDifferenceMillis / (60 * 60 * 1000);
                                long minutes = (timeDifferenceMillis % (60 * 60 * 1000)) / (60 * 1000);

                                System.out.println("时间差（小时）: " + hours);
                                System.out.println("时间差（分钟）: " + minutes);

                                if (hours == 0 && minutes == 0) {
                                    machine.setMachineStatus("1");
                                    hyrjMachineService.updateHyrjMachine(machine);
                                }
                            }
                        }
                    }
                }
            }
        }
    }


}
